/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rdr.geo.calc;

import rdr.geo.entities.Coordinate;

/**
 *
 * @author ruanor
 */
public class Haversine {
    public static double Distance(Coordinate c1, Coordinate c2)
    {
        double R = 6371;
        double dLat = c1.getLat().getValueRad() - c2.getLat().getValueRad();
        double dLon = c1.getLon().getValueRad() - c2.getLon().getValueRad();
        double lat1 = c1.getLat().getValueRad();
        double lat2 = c2.getLat().getValueRad();
        
        double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                   Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
        double d = R * c;
        
        return d;
    }
}
